//
//  RCModifyPasswordVC.m
//  RocketProject
//
//  Created by chen jiedan on 13-7-2.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCModifyPasswordVC.h"
#import "UIColor-Expanded.h"
#import "CommonDefines.h"
#import "IManager+LoginRequest.h"
#import "GloableEngineService.h"
#import "AppDelegate.h"

@interface RCModifyPasswordVC ()<IManagerDelegate,UITextFieldDelegate>{
    UITextField *_oldPassword;
    UITextField *_newPass;
    UITextField *_newConfirmPass;
    int64_t     _requestID;
}

@end

@implementation RCModifyPasswordVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.titleLabelText = @"修改密码";
    self.navigationItem.titleView = [self createTitleView];
    
    UILabel *nickName = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    nickName.text = [[GloableEngineService sharedInstance] nickName];
    [nickName setFont:[UIFont fontWithName:kFontNameLightHeiti size:15]];
    [nickName setTextColor:[UIColor colorWithHex:0xff505050]];
    [nickName setBackgroundColor:[UIColor colorWithHex:0xfff4f4f4]];
    [nickName setTextAlignment:NSTextAlignmentCenter];
    [self.viewContainer addSubview:nickName];
    [nickName release];
    
    UIImageView *bg3 = [[[UIImageView alloc] initWithFrame:CGRectMake(14, 50+15, 320-14*2, 47)] autorelease];
    UIImage *img3 = [UIImage imageNamed:@"form_bg3"];
    bg3.image = img3;
    [self.viewContainer addSubview:bg3];
    
    _oldPassword = [[UITextField alloc] initWithFrame:CGRectMake(40, 80, 260, 20)]; //50 +15
    [self.viewContainer addSubview:_oldPassword];
    _oldPassword.secureTextEntry = YES;
    _oldPassword.placeholder = @"请输入旧密码";
    [_oldPassword addTarget:self
                 action:@selector(nextStep:)
       forControlEvents:UIControlEventEditingDidEndOnExit];
    [_oldPassword setReturnKeyType:UIReturnKeyNext];
    _oldPassword.font = [UIFont fontWithName:kFontNameLightHeiti size:16];
    _oldPassword.clearButtonMode = UITextFieldViewModeWhileEditing;
    [_oldPassword release];
    _oldPassword.delegate = self;
    
    UIImageView *bg1 = [[[UIImageView alloc] initWithFrame:CGRectMake(14, 120, 320-14*2, 47)] autorelease];
    UIImage *img1 = [UIImage imageNamed:@"form_bg1"];
    bg1.image = img1;
    [self.viewContainer addSubview:bg1];

    _newPass = [[UITextField alloc] initWithFrame:CGRectMake(40, 120+15, 260, 20)]; //50 +15
    [self.viewContainer addSubview:_newPass];
    _newPass.placeholder = @"请输入新密码";
    _newPass.clearButtonMode = UITextFieldViewModeWhileEditing;
    [_newPass addTarget:self
                        action:@selector(nextStep:)
              forControlEvents:UIControlEventEditingDidEndOnExit];
    _newPass.secureTextEntry = YES;
    [_newPass setReturnKeyType:UIReturnKeyNext];
    _newPass.font = [UIFont fontWithName:kFontNameLightHeiti size:16];
    [_newPass release];
    _newPass.delegate = self;

    UIImageView *bg2 = [[[UIImageView alloc] initWithFrame:CGRectMake(14, 120+47, 320-14*2, 47)] autorelease];
    UIImage *img2 = [UIImage imageNamed:@"form_bg2"];
    bg2.image = img2;

    [self.viewContainer addSubview:bg2];
    
    _newConfirmPass = [[UITextField alloc] initWithFrame:CGRectMake(40, 120+47+15, 260, 20)]; //50 +15
    _newConfirmPass.placeholder = @"请再次输入新密码";
    [self.viewContainer addSubview:_newConfirmPass];
    [_newConfirmPass setReturnKeyType:UIReturnKeyDone];
    [_newConfirmPass addTarget:self
                       action:@selector(confirmPress:)
             forControlEvents:UIControlEventEditingDidEndOnExit];
    _newConfirmPass.secureTextEntry = YES;
    _newConfirmPass.font = [UIFont fontWithName:kFontNameLightHeiti size:16];
    _newConfirmPass.clearButtonMode = UITextFieldViewModeWhileEditing;
    [_newConfirmPass release];
    _newConfirmPass.delegate = self;

    UIButton *confirmBtn = [[UIButton alloc] initWithFrame:CGRectMake(14, 167+47+20, 320-28, 40)];
    [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [self.viewContainer addSubview:confirmBtn];
    UIImage *confirmIMG = [UIImage imageNamed:@"login_btn"];
    [confirmBtn setBackgroundImage:confirmIMG forState:UIControlStateNormal];
    //        [loginBtn setBackgroundColor:[UIColor blackColor]];
    [confirmBtn addTarget:self action:@selector(confirmPress:) forControlEvents:UIControlEventTouchUpInside];
    [confirmBtn release];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void) nextStep:(id) sender
{
    if (sender == _oldPassword) {
        [_newPass becomeFirstResponder];
    }else if (sender == _newPass){
        [_newConfirmPass becomeFirstResponder];
    }
}
- (void) confirmPress:(id) sender
{
    [_oldPassword resignFirstResponder];
    [_newPass resignFirstResponder];
    [_newConfirmPass resignFirstResponder];
    
    if ([_newPass.text isEqualToString:_newConfirmPass.text]) {
        _requestID = [[IManager sharedInstance] postModify:_oldPassword.text withPassword:_newPass.text andBduss:[[GloableEngineService sharedInstance] bduss] withDelegate:self];
    }
}

- (void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_requestID];
}

#pragma mark IManager Delegate
- (void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    
}

- (void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    RCLoginDataModel *data = (RCLoginDataModel *)dataModel;
    if (data.errorNO == 0) {
        [[GloableEngineService sharedInstance] saveUserLoginInfo:data];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }else{
        [RCAppDelegate alertWithCustomizeString:@"修改密码失败"];
    }
}


- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if ([_newPass isEqual:textField] || [_newConfirmPass isEqual:textField]) {
        self.viewContainer.frame = CGRectMake(0, -70, 320, self.viewContainer.frame.size.height);
    }else{
        self.viewContainer.frame = CGRectMake(0, 0, 320, self.viewContainer.frame.size.height);
    }
}


@end
